<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
	
	public function index () {
		$this->display();
	}

	public function logout () {
		session(null);
		$this->redirect('/admin');
	}

	public function check () {
		$username = I('post.nameTex');
		$password = I('post.password');
		$verifycode = I('post.verifycode');

		if (empty($username) || empty($password)) {
			$this->error('非法请求', '/admin');
		}

		$verify = new \Think\Verify(); 
		if(!$verify->check($verifycode, '')){
			$this->error('验证码错误','/admin');  
		}

		$admin = M('admin');
		$where['usernme'] = $username;
		$where['password'] = md5($password);

		$admin_info=$admin->where($where)->find();

		


		if ($admin_info['id']>0) {
			//写入session
			session('admin_id', $admin_info['id']);
			session('username', $admin_info['username']);

			$time = time();
			$ip = get_client_ip();
			//更新登录时间，次数 ip
			$l['id'] = $admin_info['id'];
			$l['logintime'] = $time;
			$l['loginip'] = $ip;
			$l['login_num'] = $admin_info['login_num']+1;


			$admin->save($l);


			$this->redirect('/admin/home');
		} else {
			$this->error('账号或密码错误', '/admin');
		}
	}
}
